<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 主体 -->
    <el-card class="box-card">
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryinfo.query"
            clearable
            @input="seach"
            @clear="seach"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="seach"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="10" class="clo">
          <el-button type="primary" @click="dialogFormVisible = true"
            >添加用户</el-button
          >
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table :data="table" border>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="username" label="姓名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column prop="mg_state" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              @change="guanli(scope.row.id)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="bj(scope.row.id)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="del(scope.row.id)"
            ></el-button>
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-star-off"
              @click="fenpei(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加用户的模态框 -->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item
          label="用户名"
          prop="username"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          prop="password"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile" :label-width="formLabelWidth">
          <el-input v-model="form.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="login">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改用户的模态框 -->
    <el-dialog title="编辑用户" :visible.sync="bjdialogFormVisible">
      <el-form :model="bjform" :rules="rules" ref="ruleForm">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input
            v-model="bjform.username"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item prop="email" label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="bjform.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="mobile" label="电话" :label-width="formLabelWidth">
          <el-input v-model="bjform.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="bjdialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="xiugai">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分配权限的模态框 -->
    <el-dialog title="分配角色" :visible.sync="qxdialogFormVisible">
      <el-form :model="qxform">
        <el-form-item>
          当前的用户:<span>{{ qxform.username }}</span>
        </el-form-item>
        <el-form-item>
          当前的角色:<span>{{ qxform.role }}</span>
        </el-form-item>
        <el-form-item label="分配新角色" :label-width="formLabelWidth">
          <el-select placeholder="请选择" v-model="rid">
            <el-option
              v-for="item in roles"
              :key="item.value"
              :label="item.roleName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="qxdialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="qx">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import _ from 'lodash'
import {
  user,
  tianjia,
  shanchu,
  zhuangtai,
  bianji,
  xg,
  rolename,
  usersjiao
} from '../http/api'
export default {
  data() {
    const emaillist = (rule, value, callback) => {
      const res =
        /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/
      if (!res.test(value)) {
        return callback(new Error('邮箱格式不正确'))
      }
      callback()
    }
    const mobilelist = (rule, value, callback) => {
      const res = /^1[3456789]\d{9}$/
      if (!res.test(value)) {
        return callback(new Error('手机号格式不正确'))
      }
      callback()
    }
    return {
      table: [], // 用户列表的数据
      queryinfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0, // 总条数
      rules: {
        // 验证数据
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: emaillist, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { validator: mobilelist, trigger: 'blur' }
        ]
      },
      dialogFormVisible: false, // 添加用户模态框的显示与隐藏
      form: {
        // 用户的数据
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      formLabelWidth: '120px',
      bjdialogFormVisible: false, // 修改用户模态框的显示与隐藏
      bjform: {},
      // 分配权限的内容
      qxform: {
        username: '',
        id: 0,
        role_name: ''
      },
      roles: {
        roleName: '',
        roleDesc: ''
      },
      rid: '',
      qxdialogFormVisible: false // 分配权限的模态框的显示与隐藏
    }
  },
  created() {
    this.gettable()
  },
  methods: {
    handleSizeChange(val) {
      this.queryinfo.pagesize = val
      this.gettable()
    },
    handleCurrentChange(val) {
      this.queryinfo.pagenum = val
      this.gettable()
    },
    // 搜索防抖
    seach: _.debounce(function () {
      this.gettable()
    }, 1000),
    //   请求用户数据的列表
    async gettable() {
      const res = await user(this.queryinfo)
      const { users, total } = res
      this.table = users
      this.total = total
    },
    // 添加用户
    async login() {
      const res = await tianjia(this.form)
      this.gettable()
      this.form = ''
      this.dialogFormVisible = false
    },
    // 删除用户
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await shanchu(id)
          this.gettable()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 修改状态
    async guanli(uid, mg_state) {
      await zhuangtai(uid, mg_state)
    },
    // 查询用户
    async bj(id) {
      const res = await bianji(id)
      this.bjform = res
      this.bjdialogFormVisible = true
    },
    // 编辑用户
    async xiugai() {
      await xg(this.bjform.id, this.bjform)
      this.gettable()
      this.bjdialogFormVisible = false
    },
    // 点击分配按钮
    async fenpei(row) {
      this.qxdialogFormVisible = true
      this.qxform = {
        id: row.id,
        username: row.username,
        role: row.role_name
      }
      const res = await rolename(this.qxform)
      this.roles = res
    },
    // 分配权限
    async qx() {
      await usersjiao(this.qxform.id, this.rid)
      this.gettable()
      this.qxdialogFormVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.clo {
  margin-left: 10px;
}
.el-table {
  margin-top: 16px;
}
</style>
